package cn.john.user.controller;


import cn.john.basic.exception.BusinessException;
import cn.john.basic.util.AjaxResult;
import cn.john.user.dto.LoginDto;
import cn.john.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @RequestMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        // 登录成功需要返回token和loginInfo信息，封装resultObj
        try {
            Map<String,Object> map = loginService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("系统繁忙，请稍后重试!");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> param){
        // 登录成功需要返回token和loginInfo信息，封装resultObj
        try {
            return loginService.wechatLogin(param.get("code"));
        } catch (BusinessException e){
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("系统繁忙，请稍后重试!");
        }
    }

    @PostMapping("/binder/wechat")
    public AjaxResult binderWechat(@RequestBody Map<String,String> param){
        // 登录成功需要返回token和loginInfo信息，封装resultObj
        try {
            return loginService.binderWechat(param);
        } catch (BusinessException e){
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("系统繁忙，请稍后重试!");
        }
    }
}
